<template>
  <div class="container">
    <div>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form ref="form" :model="form">
            <el-form-item label="组织机构：">
              <el-select
                v-model="groupingNo"
                placeholder="请选择分组"
                @change="groupchange"
              >
                <el-option
                  v-for="(item, index) in groupingList"
                  :value="item.CLIENT_GROUP_NO"
                  :label="item.CLIENT_GROUP_NAME"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="作业公司：">
              <el-select
                v-model="companyName"
                placeholder="请选择作业公司"
                @change="groupchange"
              >
                <el-option
                  v-for="(item, index) in companyList"
                  :value="item.作业公司"
                  :label="item.作业公司"
                  :key="index"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6">
          <el-form ref="form" :model="form">
            <el-form-item label="道路名称：">
              <el-input v-model="form.roadName" placeholder="请输入道路名称" clearable style="width: 200px"></el-input>
            </el-form-item>
 
            <el-form-item label="查询日期：">
              <!-- <el-radio v-model="form.radioData" label="日">按日期：</el-radio> -->
              <el-date-picker v-model="form.valueDay" type="date" placeholder="选择日期" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6">
          <el-form ref="form" :model="form" style="float:left">
            <el-button size="mini" type="primary" @click="getlistdata">查 询</el-button>
            <el-button size="mini" type="success" @click="exportExcel">导 出</el-button>
          </el-form>
        </el-col>
      </el-row>
      <el-row :gutter="0" style="margin-top: 20px; margin-bottom: 20px">
        <el-col :span="3" :offset="4">
          <span>{{ formInfo.all }}</span>
        </el-col>
        <el-col :span="3">
          <span>{{ formInfo.work }}</span>
        </el-col>
        <el-col :span="3">
          <span>{{ formInfo.one }}</span>
        </el-col>
        <el-col :span="3">
          <span>{{ formInfo.two }}</span>
        </el-col>
        <el-col :span="3">
          <span>{{ formInfo.three }}</span>
        </el-col>
      </el-row>
    </div>
    <el-table v-loading="loading" :data="tableData.slice((currentPage-1)*pageSize,currentPage*pageSize)" highlight-current-row>
      <!-- ROADNAME	REMARKS	LWORK1	LWORKBFB1
道路名称	作业公司	作业长度（米）	百分比 -->
      <!-- <el-table-column prop="TITLE1" label="班组名称" align="center"></el-table-column> -->
      <el-table-column prop="ROADNAME" label="道路名称" align="center"></el-table-column>
      <el-table-column prop="REMARKS" label="作业公司" align="center"></el-table-column>
      <el-table-column prop="LWORK1" label="作业长度（米）" align="center"></el-table-column>
      <el-table-column prop="LWORKBFB1" label="百分比" align="center"></el-table-column>
      <!-- <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-info" @click="handleEdit(scope.$index, scope.row)">详情</el-button>
        </template>
      </el-table-column>-->
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="tableData.length"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import '@/assets/css/common.css'
import { formatDate } from '@/common/js/date.js'
import export2Excel from '@/utils/exportExcel/index.js'
export default {
  data () {
    return {
      currentPage: 1, //初始页
      pageSize: 10,    //    每页的数据
      loading: false,
      form: {
        radioData: '日',
        valueDay: formatDate(new Date(new Date() - 3600 * 1000 * 24), 'yyyy-MM-dd'),
        valueMonth: '',
        roadName: '',
      },
      formInfo: {
        all: '',
        work: '',
        one: '',
        two: '',
        three: '',
      },
      teamList: [],
      teamValue: '',
      tableData: [],
      groupingList: [],
      groupingNo: '',
      companyList: [],
      companyName: '全部',

    }
  },
  created () {
    this.GetWorkRoadCompanyInfo();
    this.getGroupingList();
  },
  methods: {
    async GetWorkRoadCompanyInfo () {
      var url =
        "Vehicle/GetWorkRoadCompanyInfo?clientNo=" + localStorage.getItem("CLIENT_NO"); // zxdata
      let { data } = await this.$Http.get(url);
      this.companyList = data;
      this.companyList.unshift({ "作业公司": "全部" })
    },
    // 获得分组列表
    async getGroupingList () {
      var url =
        "Vehicle/GetClientInfo?client_no=" + localStorage.getItem("CLIENT_NO"); // zxdata
      let { data } = await this.$Http.get(url);
      this.groupingList = data;
      this.groupingNo = data[0].CLIENT_GROUP_NO;
      this.getlistdata();
    },
    async getlistdata () {
      this.currentPage = 1;
      var date = ''
      if (this.form.radioData === '日') {
        date = this.form.valueDay
      }
      if (this.form.radioData === '月') {
        date = this.form.valueMonth
      }
      var company = this.companyName
      if (this.companyName === '全部') {
        company = ''
      }
      var url = 'Vehicle/GetExcelCar6?clientNo=' + this.groupingNo + '&company=' + company + '&date=' + date
      // var url = 'Vehicle/GetExcelCar2?clientName=' + this.groupingNo + '&date=' + this.form.valueDay
      const { data } = await this.$Http.get(url)
      this.exportExcelData = data
      for(var i = 0; i < data.length; ) {
        if (data[i].TITLE.includes('总计')) {
          this.formInfo.all = data[i].TITLE
        }
        var title = data[i].TITLE1
        if (title.includes('未作业道路')) {
          this.formInfo.work = title
        }
        if (title.includes('主干')) {
          this.formInfo.one = title
        }
        if (title.includes('次干')) {
          this.formInfo.two = title
        }
        if (title.includes('支路')) {
          this.formInfo.three = title
        }
        if(data[i].ROADNAME === '道路名称' || data[i].ROADNAME === '') {
          data.splice(i, 1);
        } else {
            i++;  //只有在没有删除元素时才对索引 i++
        }
      }
      this.tableData = data
    if (this.form.roadName != '') this.tableData = this.tableData.filter(item => item.ROADNAME.includes(this.form.roadName))
    },
    // 导出Excel
    exportExcel () {
      let excelName = '道路未作业统计'
      const tHeader = ['班组名称', '道路名称', '作业人员', '作业长度（米）', '百分比']
      const filterVal = ['TITLE1', 'ROADNAME', 'REMARKS', 'LWORK1', 'LWORKBFB1']
      export2Excel(tHeader, filterVal, this.tableData, excelName)
    },
    //详情
    handleEdit (index, row) {
      //   this.idx = index;
      //   this.form = row;
      //   this.editVisible = true;
    },
    // 分页导航
    handlePageChange (currentPage) {
      this.currentPage = currentPage;
      // console.log(this.currentPage)
      // this.$set(this.query, 'pageIndex', val);
      // this.getData();
    },
    handleSizeChange (size) {
      this.pageSize = size;
      console.log(this.pageSize)
    }
  }
}
</script>
<style>
.content {
  overflow-y: scroll;
}
.el-col-6 {
  min-width: 400px;
  min-height: 50px;
}
.el-table th {
  height: 40px;
}
</style>